<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
        let res = JSON.parse(xhr.responseText);
        console.log(res);
      }
    }

    // 设置请求超时时间
    xhr.timeout = 15;
    xhr.ontimeout = function () {
      // 超过 15毫秒，响应结果还没有得到，就会触发这个超时事件
      alert('请求超时，请刷新重试');
    }

    xhr.open('GET', 'http://www.liulongbin.top:3006/api/getbooks');
    xhr.send();
  </script>
</body>

</html>